from commons.controller.base_controller import BaseController
from report_service.report.service.report_service import ReportService
from datetime import datetime
from flask import request


class ReportController(BaseController):
    def __init__(self):
        self.report_service = ReportService()

    def get_bar_chart_data(self):
        data = request.json
        start = data.get('start')
        end = data.get('end')
        try:
            start_date = datetime.strptime(start, "%Y-%m-%d")
            end_date = datetime.strptime(end, "%Y-%m-%d")
            result = self.report_service.get_bar_data(start_date, end_date)
            return self.success_response(result)
        except ValueError as e:
            return self.error_response(str(e))
        except Exception as e:
            return self.error_response(str(e))

    def get_pie_chart_data(self):
        data = request.json
        start = data.get('start')
        end = data.get('end')
        try:
            start_date = datetime.strptime(start, "%Y-%m-%d")
            end_date = datetime.strptime(end, "%Y-%m-%d")
            result = self.report_service.get_pie_data(start_date, end_date)
            return self.success_response(result)
        except ValueError as e:
            return self.error_response(str(e))
        except Exception as e:
            return self.error_response(str(e))

    def get_map_chart_data(self):
        data = request.json
        start = data.get('start')
        end = data.get('end')
        try:
            start_date = datetime.strptime(start, "%Y-%m-%d")
            end_date = datetime.strptime(end, "%Y-%m-%d")
            result = self.report_service.get_map_data(start_date, end_date)
            return self.success_response(result)
        except ValueError as e:
            return self.error_response(str(e))
        except Exception as e:
            return self.error_response(str(e))